
import datetime

from google.appengine.ext import db

#App imports
import models
import budget
import appuser

def GetActualExpenseByCategories(pSelectedYear):
  user = appuser.GetUser()
   
  html =  '<div class="Label">Please enter a expense budgetExp for year: ' + budget.GetBudgetYears() + '</div>\r\n'
  html += '<br/>\r\n'
  html += '<input id="txtBudgetAction" name="txtBudgetAction" class="textbox" type="hidden" value="" />\r\n'
  html += '<div class="Content">\r\n'
  html += '<table cellpadding="2" cellspacing="2">\r\n'
  html += '<tr>\r\n'
  html += '<td class="ExpHeaderDel">&nbsp;</td>\r\n'
  html += '<td class="ExpHeaderText">Expense Category</td>\r\n'
  html += '<td class="ExpHeaderNum">Total(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Total(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Jan(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Jan(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Feb(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Feb(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Mar(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Mar(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Apr(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Apr(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">May(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">May(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Jun(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Jun(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Jul(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Jul(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Aug(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Aug(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Sep(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Sep(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Oct(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Oct(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Nov(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Nov(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Dec(a)</td>\r\n'
  html += '<td class="ExpHeaderNum">Dec(b)</td>\r\n'
  html += '</tr>\r\n'
  html += '<tr>\r\n'
  html += '<td class="ExpHeaderDel">&nbsp;</td>\r\n'
  html += '<td class="ExpHeaderText">Total</td>\r\n'
  html += '<td class="ExpDataTotalNum"><--TOTALA--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--TOTALB--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--JANATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--JANBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--FEBATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--FEBBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--MARATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--MARBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--APRATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--APRBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--MAYATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--MAYBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--JUNATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--JUNBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--JULATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--JULBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--AUGATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--AUGBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--SEPATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--SEPBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--OCTATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--OCTBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--NOVATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--NOVBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--DECATOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--DECBTOTAL--></td>\r\n'
  html += '</tr>\r\n'


  selectedCategories = ''
  janTotalB = 0
  febTotalB = 0
  marTotalB = 0
  aprTotalB = 0
  mayTotalB = 0
  junTotalB = 0
  julTotalB = 0
  augTotalB = 0
  sepTotalB = 0
  octTotalB = 0
  novTotalB = 0
  decTotalB = 0
  totalB = 0
  totalCatB = 0
  janTotalA = 0
  febTotalA = 0
  marTotalA = 0
  aprTotalA = 0
  mayTotalA = 0
  junTotalA = 0
  julTotalA = 0
  augTotalA = 0
  sepTotalA = 0
  octTotalA = 0
  novTotalA = 0
  decTotalA = 0
  janA = 0
  febA = 0
  marA = 0
  aprA = 0
  mayA = 0
  junA = 0
  julA = 0
  augA = 0
  sepA = 0
  octA = 0
  novA = 0
  decA = 0
  totalA = 0
  totalCatA = 0
  index = 0

  budgetExpDao = budget.BudgetDao()
  actualDao = ActualDao()
  
  budgetExps = budgetExpDao.GetBudgetExpByUserYear(user.key(), pSelectedYear)

  for budgetExp in budgetExps:

    index += 1
    janTotalB += budgetExp.JanExp
    febTotalB += budgetExp.FebExp
    marTotalB += budgetExp.MarExp
    aprTotalB += budgetExp.AprExp
    mayTotalB += budgetExp.MayExp
    junTotalB += budgetExp.JunExp
    julTotalB += budgetExp.JulExp
    augTotalB += budgetExp.AugExp
    sepTotalB += budgetExp.SepExp
    octTotalB += budgetExp.OctExp
    novTotalB += budgetExp.NovExp
    decTotalB += budgetExp.DecExp

    totalCatB = budgetExp.JanExp + budgetExp.FebExp + budgetExp.MarExp + budgetExp.AprExp + budgetExp.MayExp + budgetExp.JunExp + budgetExp.JulExp
    totalCatB += budgetExp.AugExp + budgetExp.SepExp + budgetExp.OctExp + budgetExp.NovExp + budgetExp.DecExp

    totalB += totalCatB

    actualExpenses = actualDao.GetActualExpByCatUserYear(budgetExp.Category.CategoryName, user.key(), pSelectedYear)

    janA = 0
    febA = 0
    marA = 0
    aprA = 0
    mayA = 0
    junA = 0
    julA = 0
    augA = 0
    sepA = 0
    octA = 0
    novA = 0
    decA = 0

    for actual in actualExpenses:
      janA = actual.JanExp
      febA = actual.FebExp
      marA = actual.MarExp
      aprA = actual.AprExp
      mayA = actual.MayExp
      junA = actual.JunExp
      julA = actual.JulExp
      augA = actual.AugExp
      sepA = actual.SepExp
      octA = actual.OctExp
      novA = actual.NovExp
      decA = actual.DecExp
   


    janTotalA += janA
    febTotalA += febA
    marTotalA += marA
    aprTotalA += aprA
    mayTotalA += mayA
    junTotalA += junA
    julTotalA += julA
    augTotalA += augA
    sepTotalA += sepA
    octTotalA += octA
    novTotalA += novA
    decTotalA += decA

    totalCatA = janA + febA + marA + aprA + mayA + junA + julA + augA + sepA + octA + novA + decA;

    totalA += totalCatA


    html += '<tr>\r\n'
    html += '<td class="ExpDataDel"><a href="">X</a></td>\r\n'
    html += '<td class="ExpDataCat">\r\n';
    html += '<input id="txtExpCatName' + str(index) + '" name="txtExpCatName' + str(index) + '" class="textbox" type="hidden" value="' + budgetExp.Category.CategoryName.lower() + '" />\r\n'
    html += '<input id="txtBudgetCatKey' + str(index) + '" name="txtBudgetCatKey' + str(index) + '" class="textbox" type="hidden" value="' + str(budgetExp.key()) + '" />\r\n'
    html += budgetExp.Category.CategoryName.upper() + '</td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(totalCatA,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(totalCatB,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(janA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.JanExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(febA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.FebExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(marA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.MarExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(aprA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.AprExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(mayA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.MayExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(junA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.JunExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(julA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.JulExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(augA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.AugExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(sepA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.SepExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(octA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.OctExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(novA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.NovExp,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><a href="doActualExpDetail()">' + str(long(round(decA,0))) + '</a></td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(budgetExp.DecExp,0))) + '</td>\r\n'
    html += '</tr>\r\n'
    
  html += '</table>\r\n'
  html += '</div>\r\n'
  html += '<input id="txtBudgetIndex" name="txtBudgetIndex" class="textbox" type="hidden" value="' + str(index) + '" />\r\n'

  html = html.replace('<--DDLCATEGORY-->', budget.GetBudgetExpenseCategoriesNotUsed(selectedCategories))
  html = html.replace('<--TOTALB-->', str(long(round(totalB,0)))).replace('<--JANBTOTAL-->', str(long(round(janTotalB,0))))
  html = html.replace('<--FEBBTOTAL-->', str(long(round(febTotalB,0)))).replace('<--MARBTOTAL-->', str(long(round(marTotalB,0))))
  html = html.replace('<--APRBTOTAL-->', str(long(round(aprTotalB,0)))).replace('<--MAYBTOTAL-->', str(long(round(mayTotalB,0))))
  html = html.replace('<--JUNBTOTAL-->', str(long(round(junTotalB,0)))).replace('<--JULBTOTAL-->', str(long(round(julTotalB,0))))
  html = html.replace('<--AUGBTOTAL-->', str(long(round(augTotalB,0)))).replace('<--SEPBTOTAL-->', str(long(round(sepTotalB,0))))
  html = html.replace('<--OCTBTOTAL-->', str(long(round(octTotalB,0)))).replace('<--NOVBTOTAL-->', str(long(round(novTotalB,0))))
  html = html.replace('<--DECBTOTAL-->', str(long(round(decTotalB,0))))
  html = html.replace('<--TOTALA-->', str(long(round(totalA,0)))).replace('<--JANATOTAL-->', str(long(round(janTotalA,0))))
  html = html.replace('<--FEBATOTAL-->', str(long(round(febTotalA,0)))).replace('<--MARATOTAL-->', str(long(round(marTotalA,0))))
  html = html.replace('<--APRATOTAL-->', str(long(round(aprTotalA,0)))).replace('<--MAYATOTAL-->', str(long(round(mayTotalA,0))))
  html = html.replace('<--JUNATOTAL-->', str(long(round(junTotalA,0)))).replace('<--JULATOTAL-->', str(long(round(julTotalA,0))))
  html = html.replace('<--AUGATOTAL-->', str(long(round(augTotalA,0)))).replace('<--SEPATOTAL-->', str(long(round(sepTotalA,0))))
  html = html.replace('<--OCTATOTAL-->', str(long(round(octTotalA,0)))).replace('<--NOVATOTAL-->', str(long(round(novTotalA,0))))
  html = html.replace('<--DECATOTAL-->', str(long(round(decTotalA,0))))
  return html

class ActualDao():

  def GetActualExpByCatUserYear(self, pCatKey, pUserKey, pYear):
    actualExpCategory = models.ActualExpenseByCategory.gql("WHERE Category = :1 AND User = :2 AND Year = :3", pCatKey, pUserKey, pYear)
    return actualExpCategory
  
